
#include "kodes.h"
#include "utf16.h"

#include <boost/assert.hpp>

// se 0 redonis, do EOF aux alia erraro
wchar_t ElUtf16::doni()
{
  unsigned char b1 = doniBajton();
  unsigned char b2 = doniBajton();
  if (waitBOM_ && b1 == 0xff && b2 == 0xfe)
  {
    waitBOM_ = false;
    return doni();
  }
  waitBOM_ = false;
  wchar_t res = b1 + (static_cast<wchar_t>(b2) << 8);
//  if (res == 0xd)
//    return doni();
  return res;
}

void AlUtf16::setStream(ostream *s)
{
  ScriboKonverto::setStream(s);
  if (wrBOM_)
  {
    BOOST_ASSERT(s && 0 == s->tellp());
    metiBajton(0xff);
    metiBajton(0xfe);
  }
}

void AlUtf16::meti(wchar_t c)
{
  BOOST_ASSERT(c);
  metiBajton(static_cast<unsigned char>(c));
  metiBajton(static_cast<unsigned char>(c>>8));
}
